/**
 * @author: Jam
 * @create: 2018-03-26 17:13
 */
package com.byxy.action;

import com.byxy.model.AdminUser;
import com.byxy.model.PageModel;
import com.byxy.service.AdminUserService;
import com.byxy.service.AdminUserTagService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class AdminUserAction extends ActionSupport {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private AdminUserTagService adminUserTagService;

    private AdminUser adminUser = new AdminUser();
    private PageModel pageModel = new PageModel();
    private Integer id[];
    // 页数
    private Integer page;
    // 行数
    private Integer rows;
    // 模糊搜索的关键字
    private String search;

    /*
        分页查询视图
    */
    public String list() {
        if (page != null) {
            pageModel.setPage(page);
        }
        if (rows != null) {
            pageModel.setPageSize(rows);
        }
        if (search == null) {
            search = "";
        }
        Integer count = adminUserService.count(search);
        pageModel.setTotalCount(count);
        List<AdminUser> list = adminUserService.listPage(pageModel.getPage(), pageModel.getPageSize(), search);
        pageModel.setItems(list);
        return SUCCESS;
    }

    /*
        增加视图
     */
    public String add() {
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        request.setAttribute("groups", adminUserTagService.listGroup());
        return SUCCESS;
    }

    /*
        编辑视图
     */
    public String edit() {
        adminUser = adminUserService.get(id[0]);
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        request.setAttribute("groups", adminUserTagService.listGroup());
        return SUCCESS;
    }

    /*
        删除操作
     */
    public String doDelete() {
        adminUserService.delete(id);
        return SUCCESS;
    }

    /*
        启用操作
     */
    public String doEnable() {
        adminUserService.enable(id);
        return SUCCESS;
    }

    /*
        禁用操作
     */
    public String doDisable() {
        adminUserService.disable(id);
        return SUCCESS;
    }

    /*
       判断是增加还是更新, 执行对应操作
    */
    public String doAddOrEdit() {
        if (adminUser.getId() == null || adminUser.getId() == 0) {
            adminUserService.save(adminUser);
        } else {
            adminUserService.update(adminUser);
        }
        return SUCCESS;
    }

    public String doLogin() {
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        String username = "";
        String password = "";
        if (request.getParameter("username") != null) {
            username = request.getParameter("username");
        }
        if (request.getParameter("password") != null) {
            password = request.getParameter("password");
        }
        AdminUser adminUser = adminUserService.getLogin(username, password);
        if (adminUser != null) {
//            System.out.println(adminUser);
//            System.out.println(adminUser.getAdminGroup().getAdminRules());
            ActionContext ac = ActionContext.getContext();
            Map session = ac.getSession();
            session.put("adminUser", adminUser);
            return SUCCESS;
        }
        return ERROR;
    }

    /********************下面是get和set********************/

    public AdminUserService getAdminUserService() {
        return adminUserService;
    }

    public void setAdminUserService(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    public AdminUserTagService getAdminUserTagService() {
        return adminUserTagService;
    }

    public void setAdminUserTagService(AdminUserTagService adminUserTagService) {
        this.adminUserTagService = adminUserTagService;
    }

    public AdminUser getAdminUser() {
        return adminUser;
    }

    public void setAdminUser(AdminUser adminUser) {
        this.adminUser = adminUser;
    }

    public PageModel getPageModel() {
        return pageModel;
    }

    public void setPageModel(PageModel pageModel) {
        this.pageModel = pageModel;
    }

    public Integer[] getId() {
        return id;
    }

    public void setId(Integer[] id) {
        this.id = id;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }
}
